package com.example.martian.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    public static boolean filterPwd(String str) {
        String regEx = "^(?=.*[0-9])(?=.*[a-zA-Z])(.{8,20})$";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        boolean find = m.find();
        return find;
    }

    public static String autoAppendZero(String s, int type) {
        if (type == 0) {
            return new DecimalFormat("0.0000").format(new Double(s));
        } else {
            return new DecimalFormat("0.00").format(new Double(s));
        }
    }

    public static String precisionProcessing(BigDecimal bigDecimal, int accuracy) {
        if (bigDecimal == null) {
            return "0.00";
        }
        return bigDecimal.setScale(accuracy, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString();
    }

    public static String sixDecimal(String s) {
        return new DecimalFormat("0.000000").format(new Double(s));
    }

    public static String subZeroAndDot(String s) {
        if (s.contains("0E")) {
            s = "0";
        }
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");
            s = s.replaceAll("[.]$", "");
        }
        return s;
    }


    public static DecimalFormat getDecimal(int jd) {
        DecimalFormat dc = new DecimalFormat("0");
        if (jd == 0) {
            dc = new DecimalFormat("0");
        } else if (jd == 1) {
            dc = new DecimalFormat("0.0");
        } else if (jd == 2) {
            dc = new DecimalFormat("0.00");
        } else if (jd == 3) {
            dc = new DecimalFormat("0.000");
        } else if (jd == 4) {
            dc = new DecimalFormat("0.0000");
        } else if (jd == 5) {
            dc = new DecimalFormat("0.00000");
        } else {
            dc = new DecimalFormat("0.000000");
        }
        dc.setRoundingMode(RoundingMode.FLOOR);
        return dc;
    }


    public static String returnRandomNumber() {
        return String.valueOf(((int) ((Math.random() * 9 + 1) * 100000)));
    }


    public static List<String> getNewList(List<String> li) {
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < li.size(); i++) {
            String str = li.get(i);
            if (!list.contains(str)) {
                list.add(str);
            }
        }
        return list;
    }


    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 閿熸枻鎷穌ouble閿熸枻鎷峰紡閿熸枻鎷蜂负鎸囬敓鏂ゆ嫹灏忛敓鏂ゆ嫹浣嶉敓鏂ゆ嫹String閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷峰皬閿熸枻鎷蜂綅閿熸枻鎷�閿熸枻鎷峰叏
     *
     * @param v     閿熸枻鎷疯閿熸枻鎷峰紡閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹
     * @param scale 灏忛敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷蜂綅
     * @return
     */
    public static String roundByScale(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The   scale   must   be   a   positive   integer   or   zero");
        }
        if (scale == 0) {
            return new DecimalFormat("0").format(v);
        }
        String formatStr = "0.";
        for (int i = 0; i < scale; i++) {
            formatStr = formatStr + "0";
        }
        return new DecimalFormat(formatStr).format(v);
    }

    //生成随机数字和字母,  
    public static String getStringRandom(int length) {
        StringBuffer val = new StringBuffer();
        Random random = new Random();
        //参数length，表示生成几位随机数  
        int ischar = 0;
        int isnum = 0;
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            if (ischar >= 2) {
                charOrNum = "num";
            }
            if (isnum >= 4) {
                charOrNum = "char";
            }
            //输出字母还是数字  
            if ("char".equalsIgnoreCase(charOrNum)) {
                val.append((char) (random.nextInt(26) + 65));
                ischar++;
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val.append(random.nextInt(10));
                isnum++;
            }
        }
        return val.toString();
    }

    //length用户要求产生字符串的长度
    public static String getRandomString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }


    public static String decimanumber(double number) {
        DecimalFormat dc = new DecimalFormat("0.00000000");
        return dc.format(number);
    }

    public static String getPrettyNumber(String number) {
        return BigDecimal.valueOf(Double.parseDouble(number))
                .stripTrailingZeros().toPlainString();
    }

    public static String getConvertNumber(String number) {
        return BigDecimal.valueOf(Double.parseDouble(number))
                .stripTrailingZeros().toPlainString();
    }

    /**
     * 需要排序的集合-降序
     *
     * @param list -集合
     * @param key  -排序键
     * @return
     */
    public static List<Map<String, Object>> java_ListMapSort(List<Map<String, Object>> list, String key) {
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Double name1 = Double.valueOf(o1.get(key).toString());//name1是从你list里面拿出来的一个
                Double name2 = Double.valueOf(o2.get(key).toString()); //name1是从你list里面拿出来的第二个name
                return name2.compareTo(name1);
            }
        });
        return list;
    }

    public static int characterNum(String str) {

        char[] array = str.toCharArray();

        int mNum = 0, eNum = 0, cNum = 0;

        for (char c : array) {
            if (c >= '0' && c <= '9') {
                mNum++;
            } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                eNum++;
            } else {
                cNum++;
            }
        }
        return cNum + mNum + eNum;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List<Integer> formatDatas(String str) {
        String[] str1 = str.replace("data", "").replace("{", "").replace("}", "").replace("=", "").replace("[", "").replace("]", "").trim().split(",");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < str1.length; i++) {
            list.add(Integer.parseInt(str1[i]));
        }
        //去重
        HashSet h = new HashSet(list);
        list.clear();
        list.addAll(h);
        return list;
    }

    public static String gettimenumber() {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar = Calendar.getInstance();
        String dateName = df.format(calendar.getTime());
        return dateName;
    }

    /**
     * 生成指定位数的随机数
     *
     * @param length
     * @return
     */
    public static String getRandom(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            val += String.valueOf(random.nextInt(10));
        }
        return val;
    }

    public static String getOrdernumber() {
        String ordernumber = gettimenumber();
        String val = getRandom(9);
        String s = ordernumber + val;
        return s;
    }

    public static void main(String[] args) throws Exception {

        System.out.println(getOrdernumber());
//		returnRandomNumber();
    }

}
